// miniprogram/pages/richText/richText.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    content: '',
    title: '',
    upSection: '',
    nextSection: '',
    firstLink: '',
    secondLink: '',
    datasource: [],
    isLoading: false,
    scrollTop: 0,
    showSetupView: false,
    fontSize: 22,
    backgroundNO: 255,
    colorNO: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.showLoading({
      title: '加载中',
    })

    var colorNO = wx.getStorageSync('colorNO') || 0
    var backgroundNO = wx.getStorageSync('backgroundNO') || 255
    var fontSize = wx.getStorageSync('fontSize') || 22
    this.setData({
      colorNO: colorNO,
      backgroundNO: backgroundNO,
      fontSize: fontSize
    })

    const firstLink = options.firstLink
    const secondLink = options.secondLink
    var link = firstLink + secondLink
    var datasource = this.data.datasource

    if (datasource.length > 0) {
      datasource.splice(0, 1)
    }
    wx.request({
      url: 'https://m.bixiadu.com/' + link,
      success: ((res) => {
        // console.log(res.data.split('postErrorChapter')[1].split('<div')[0])
        const content = res.data.split('postErrorChapter')[1].split('<div')[0].split('</p>')[1]
        var title = res.data.split('</span>')[0].split('class="title">')[1].split('&nbsp;')[0]
        var nextSection = res.data.split('下一章')[0].split('目录</a>')[1].split('<a href="')[1].split('"')[0]
        var upSection = res.data.split('上一章')[0].split('<p class="Readpage">')[1].split('<a href="')[1].split('"')[0]
        // console.log(content)
        datasource.push({
          content: content,
          title: title
        })
        var scrollTop = wx.getStorageSync(firstLink + secondLink + 'scrollTop') || 0

        that.setData({
          datasource: datasource,
          // content: content,
          // title: title,
          isLoading: false,
          firstLink: firstLink,
          secondLink: secondLink,
          upSection: upSection,
          nextSection: nextSection,
          scrollTop: scrollTop
        })

        wx.setStorageSync(firstLink, secondLink)




        wx.hideLoading()
        // console.log(content)
        // const list = encodeURIComponent(res.data).split(encodeURIComponent('<div class="article-con">'))[1].split(encodeURIComponent('</div'))
        // console.log((list[0]))
        // for (const key in list) {
        // console.log(encodeURIComponentlist[key])
        // }
      }),
      fail: ((e) => {
        that.onLoad()
      })
    })
  },

  upSection() {
    // console.log(this.data.upSection)
    var datasource = this.data.datasource
    if (datasource.length > 0) {

      datasource.splice(0, 1)
      this.setData({
        datasource: datasource
      })
    }

    if (this.data.upSection.indexOf('Commend.html') != -1) {
      wx.showModal({
        title: '提示',
        content: '已经是第一章',
      })
    } else {
      this.onLoad({
        firstLink: this.data.firstLink,
        secondLink: this.data.upSection
      })
    }
  },
  nextSection() {
    var that = this

    // setTimeout(function(){
    wx.setStorageSync(this.data.firstLink + this.data.secondLink + 'scrollTop', 0)
    that.onLoad({
      firstLink: that.data.firstLink,
      secondLink: that.data.nextSection
    })
    // }, count)

    // wx.pageScrollTo({
    //   scrollTop: 0
    // })
  },
  bindscrolltolower() {
    if (this.data.isLoading) {
      return
    }
    this.setData({
      isLoading: true
    })
    this.nextSection()
  },
  bindscrolltoupper() {
    if (this.data.isLoading) {
      return
    }
    this.setData({
      isLoading: true
    })
    this.upSection()
  },

  onPageScroll: function (e) {
    wx.setStorageSync(this.data.firstLink + this.data.secondLink + 'scrollTop', e.detail.scrollTop)
  },

  /** 显示操作界面 */
  showSetup() {
    this.setData({
      showSetupView: !this.data.showSetupView
    })
  },

  /** 修改字体大小 */
  bindchanging(e) {
    this.setData({
      fontSize: e.detail.value
    })
    wx.setStorageSync('fontSize', this.data.fontSize)
  },
  /** 修改背景颜色 */
  bindchangingBackground(e) {
    this.setData({
      backgroundNO: e.detail.value
    })
    wx.setStorageSync('backgroundNO', this.data.backgroundNO)
  },
  /** 修改字体颜色 */
  bindchangingFontColor(e) {
    this.setData({
      colorNO: e.detail.value
    })
    wx.setStorageSync('colorNO', this.data.colorNO)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('onReachBottom')
    // this.nextSection()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})